<?php

namespace app\api\model\group;

use app\api\model\User;
use think\Db;
use think\Model;


class Group extends Model
{





    // 表名
    protected $name = 'group';

    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'integer';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';
    protected $deleteTime = false;


    // 追加属性
    protected $append = [
        'gender_text',
        'team_gender_text'
    ];

    public function getTeamGenderList()
    {
        return [0=>'女',1=>'男',2=>'不限'];
    }


    public function getTeamGenderTextAttr($value, $data)
    {
        $value = $value ?: ($data['team_gender'] ?? '');
        $list = $this->getTeamGenderList();
        return $list[$value] ?? '';
    }


    public function getGenderList()
    {
        return [0=>'女',1=>'男'];
    }

    public function getGenderTextAttr($value, $data)
    {
        $value = $value ?: ($data['gender'] ?? '');
        $list = $this->getGenderList();
        return $list[$value] ?? '';
    }


    public function apply($params)
    {
        $user_id = $params['user_id'];

        Db::startTrans();

        try {
            $cert_img = json_decode($params['cert_img'],true)?json_decode($params['cert_img'],true):_json_decode($params['cert_img']);
            $service_img = json_decode($params['service_img'],true)?json_decode($params['service_img'],true):_json_decode($params['service_img']);
            $service_video = json_decode($params['service_video'],true)?json_decode($params['service_video'],true):_json_decode($params['service_video']);
            $service_project = json_decode($params['service_project'],true)?json_decode($params['service_project'],true):_json_decode($params['service_project']);

            $model = new Group();

            $data = [
                'user_id'=>$user_id,
                'nickname'=>$params['nickname'],
                'realname'=>$params['realname'],
                'gender'=>$params['gender'],
                'age'=>$params['age'],
                'education'=>$params['education'],
                'major'=>$params['major'],
                'title'=>$params['title'],
                'mobile'=>$params['mobile'],
                'image'=>$params['image'],
                'province'=>$params['province'],
                'city'=>$params['city'],
                'district'=>$params['district'],
                'province_id'=>$params['province_id'],
                'city_id'=>$params['city_id'],
                'district_id'=>$params['district_id'],
                'hobby'=>$params['hobby'],

                'profile'=>$params['profile'],
                'name'=>$params['name'],
                'people_num'=>$params['people_num'],
                'min_age'=>$params['min_age'],
                'max_age'=>$params['max_age'],
                'team_gender'=>$params['team_gender'],
                'user_level'=>$params['user_level'],
                'activity_province'=>$params['activity_province'],
                'activity_province_id'=>$params['activity_province_id'],
                'activity_city'=>$params['activity_city'],
                'activity_city_id'=>$params['activity_city_id'],
                'activity_district'=>$params['activity_district'],
                'activity_district_id'=>$params['activity_district_id'],
                'is_activity_unlimited'=>isset($params['is_activity_unlimited'])?$params['is_activity_unlimited']:0,
                'group_notice'=>$params['group_notice'],
                'taboo_id'=>$params['taboo_id'],
                'standard_img'=>$params['standard_img'],
                'explain'=>$params['explain'],
                'notice'=>$params['notice'],

                'company_name'=>$params['company_name'],
                'business_licence_number'=>$params['business_licence_number'],
                'company_found_date'=>$params['company_found_date'],
                'company_address'=>$params['company_address'],
                'company_attr'=>$params['company_attr'],
                'company_industry'=>$params['company_industry'],
                'company_business'=>$params['company_business'],
                'company_people_num'=>$params['company_people_num'],
                'company_mobile'=>$params['company_mobile'],
                'company_intro'=>$params['company_intro'],
                'cover'=>$params['cover']
            ];

            if ($data['people_num']>500){
                throw new \Exception('团队人数不能大于500');
            }
            $user = User::get($user_id);

            if ($user_id!=37&&$user_id!=164){
                if ($user['level']==0){
                    throw new \Exception('普通用户不能操作');
                }
                if ($user->person_auth==1){
                    $count = Group::where([ 'user_id'=>$user_id])->count();
                    if ($count>=3){
                        throw new \Exception('已达到限制发布数量');
                    }
                }
            }


            /*if (empty($data['nickname'])){
                throw new \Exception('请输入昵称');
            }
            if (empty($data['realname'])){
                throw new \Exception('请输入姓名');
            }
            if (empty($data['age'])){
                throw new \Exception('请输入年龄');
            }
            if (empty($data['education'])){
                throw new \Exception('请输入学历');
            }
            if (empty($data['major'])){
                throw new \Exception('请输入专业');
            }
            if (empty($data['title'])){
                throw new \Exception('请输入职称');
            }

            if (empty($data['mobile'])){
                throw new \Exception('请输入电话');
            }
            if (empty($data['province'])||empty($data['city'])||empty($data['district'])){
                throw new \Exception('请选择区域');
            }*/

            /*if (empty($data['profile'])){
                throw new \Exception('请输入团长简介');
            }*/
            if (empty($data['name'])){
                throw new \Exception('请输入团队名称');
            }
            if (empty($data['people_num'])){
                throw new \Exception('请输入团队限制人数');
            }
            if (empty($data['min_age'])){
                throw new \Exception('请输入年龄范围的最小年龄');
            }
            if (empty($data['max_age'])){
                throw new \Exception('请输入年龄范围的最大年龄');
            }
            if (empty($data['standard_img'])){
                throw new \Exception('请上传标准照');
            }
            if ($data['is_activity_unlimited']==0){
                if (empty($data['activity_province'])||empty($data['activity_city'])||empty($data['activity_district'])){
                    throw new \Exception('请选择活动区域');
                }
            }

            if (empty($data['taboo_id'])){
                throw new \Exception('请选择入团禁忌');
            }
            if (empty($service_project)){
                throw new \Exception('请选择组团项目');
            }

            if ($data['user_level']==''){
                throw new \Exception('请选择会员等级');
            }
            /*if (empty($data['image'])){

                throw new \Exception('请上传个人照');
            }*/
            $res = $model->save($data);
            if ($res){
                //公告
                $notice_model = new GroupNotice();
                $notice_data = [
                    'group_id'=>$model->group_id,
                    'notice'=>$data['notice']
                ];
                $notice_model->save($notice_data);

                $media_model = new GroupMedia();
                $media_list = [];
                foreach ($cert_img as $k=>$v){
                    $media_list[] = [
                        'group_id'=>$model->group_id,
                        'type'=>0,
                        'media'=>$v['media'],
                        'title'=>$v['title'],
                        'describe'=>$v['describe']
                    ];
                }
                foreach ($service_img as $k=>$v){
                    $media_list[] = [
                        'group_id'=>$model->group_id,
                        'type'=>1,
                        'media'=>$v['media'],
                        'title'=>$v['title'],
                        'describe'=>$v['describe']
                    ];
                }
                foreach ($service_video as $k=>$v){
                    $media_list[] = [
                        'group_id'=>$model->group_id,
                        'type'=>2,
                        'media'=>$v['media'],
                        'title'=>$v['title'],
                        'describe'=>$v['describe']
                    ];
                }
                $media_model->saveAll($media_list);

                $apply_model = new GroupProjectApply();

                $service_project_list = [];
                foreach ($service_project as $k=>$v){
                    $service_project_list[] = [
                        'group_id'=>$model->group_id,
                        'project_id'=>$v['id'],
                        'category_id'=>$v['category_id'],
                        'name'=>$v['name']
                    ];
                }
                $apply_model->saveAll($service_project_list);
            }
            Db::commit();
            return $model->group_id;
        } catch (\Exception $e) {
            // 回滚事务
            Db::rollback();
            throw new \Exception($e->getMessage());
        }

    }


    public function edit($params)
    {
        $group_id = $params['group_id'];

        //$user_id = $params['user_id'];

        Db::startTrans();

        try {
            $cert_img = json_decode($params['cert_img'],true)?json_decode($params['cert_img'],true):_json_decode($params['cert_img']);
            $service_img = json_decode($params['service_img'],true)?json_decode($params['service_img'],true):_json_decode($params['service_img']);
            $service_video = json_decode($params['service_video'],true)?json_decode($params['service_video'],true):_json_decode($params['service_video']);
            $service_project = json_decode($params['service_project'],true)?json_decode($params['service_project'],true):_json_decode($params['service_project']);

            $model = new Group();
            $data = [
                'nickname'=>$params['nickname'],
                'realname'=>$params['realname'],
                'gender'=>$params['gender'],
                'age'=>$params['age'],
                'education'=>$params['education'],
                'major'=>$params['major'],
                'title'=>$params['title'],
                'mobile'=>$params['mobile'],
                'image'=>$params['image'],
                'province'=>$params['province'],
                'city'=>$params['city'],
                'district'=>$params['district'],
                'province_id'=>$params['province_id'],
                'city_id'=>$params['city_id'],
                'district_id'=>$params['district_id'],
                'hobby'=>$params['hobby'],

                'profile'=>$params['profile'],
                'name'=>$params['name'],
                'people_num'=>$params['people_num'],
                'min_age'=>$params['min_age'],
                'max_age'=>$params['max_age'],
                'team_gender'=>$params['team_gender'],
                'user_level'=>$params['user_level'],
                'activity_province'=>$params['activity_province'],
                'activity_province_id'=>$params['activity_province_id'],
                'activity_city'=>$params['activity_city'],
                'activity_city_id'=>$params['activity_city_id'],
                'activity_district'=>$params['activity_district'],
                'activity_district_id'=>$params['activity_district_id'],
                'is_activity_unlimited'=>isset($params['is_activity_unlimited'])?$params['is_activity_unlimited']:0,
                'group_notice'=>$params['group_notice'],
                'taboo_id'=>$params['taboo_id'],
                'standard_img'=>$params['standard_img'],
                'explain'=>$params['explain'],
                'notice'=>$params['notice'],

                'company_name'=>$params['company_name'],
                'business_licence_number'=>$params['business_licence_number'],
                'company_found_date'=>$params['company_found_date'],
                'company_address'=>$params['company_address'],
                'company_attr'=>$params['company_attr'],
                'company_industry'=>$params['company_industry'],
                'company_business'=>$params['company_business'],
                'company_people_num'=>$params['company_people_num'],
                'company_mobile'=>$params['company_mobile'],
                'company_intro'=>$params['company_intro'],
                'cover'=>$params['cover']
            ];

            /*$user = User::get($user_id);
            if ($user->person_auth==1){
                $count = Group::where([ 'user_id'=>$user_id])->count();
                if ($count>=3){
                    throw new \Exception('已达到限制发布数量');
                }
            }*/

            /*if (empty($data['nickname'])){
                throw new \Exception('请输入昵称');
            }
            if (empty($data['realname'])){
                throw new \Exception('请输入姓名');
            }
            if (empty($data['age'])){
                throw new \Exception('请输入年龄');
            }
            if (empty($data['education'])){
                throw new \Exception('请输入学历');
            }
            if (empty($data['major'])){
                throw new \Exception('请输入专业');
            }
            if (empty($data['title'])){
                throw new \Exception('请输入职称');
            }

            if (empty($data['mobile'])){
                throw new \Exception('请输入电话');
            }
            if (empty($data['province'])||empty($data['city'])||empty($data['district'])){
                throw new \Exception('请选择区域');
            }*/

            /*if (empty($data['profile'])){
                throw new \Exception('请输入团长简介');
            }*/
            if (empty($data['name'])){
                throw new \Exception('请输入团队名称');
            }
            if (empty($data['people_num'])){
                throw new \Exception('请输入团队限制人数');
            }
            if (empty($data['min_age'])){
                throw new \Exception('请输入年龄范围的最小年龄');
            }
            if (empty($data['max_age'])){
                throw new \Exception('请输入年龄范围的最大年龄');
            }
            if (empty($data['standard_img'])){
                throw new \Exception('请上传标准照');
            }
            if ($data['is_activity_unlimited']==0){
                if (empty($data['activity_province'])||empty($data['activity_city'])||empty($data['activity_district'])){
                    throw new \Exception('请选择活动区域');
                }
            }


            if (empty($service_project)){
                throw new \Exception('请选择组团项目');
            }
            /*if (empty($data['image'])){

                throw new \Exception('请上传个人照');
            }*/
            $res = $model->save($data,['group_id'=>$group_id]);
            if ($res){
                GroupMedia::destroy(['group_id'=>$group_id]);
                $media_model = new GroupMedia();
                $media_list = [];
                foreach ($cert_img as $k=>$v){
                    $media_list[] = [
                        'group_id'=>$group_id,
                        'type'=>0,
                        'media'=>$v['media'],
                        'title'=>$v['title'],
                        'describe'=>$v['describe']
                    ];
                }

                foreach ($service_img as $k=>$v){
                    $media_list[] = [
                        'group_id'=>$group_id,
                        'type'=>1,
                        'media'=>$v['media'],
                        'title'=>$v['title'],
                        'describe'=>$v['describe']
                    ];
                }
                foreach ($service_video as $k=>$v){
                    $media_list[] = [
                        'group_id'=>$group_id,
                        'type'=>2,
                        'media'=>$v['media'],
                        'title'=>$v['title'],
                        'describe'=>$v['describe']
                    ];
                }
                $media_model->saveAll($media_list);

                //GroupProjectApply::destroy(['group_id'=>$group_id]);
                $apply_model = new GroupProjectApply();
                $service_project_list = [];
                foreach ($service_project as $k=>$v){
                    $info = GroupProjectApply::get(['group_id'=>$group_id]);
                    if ($info){
                        $info->project_id = $v['id'];
                        $info->category_id = $v['category_id'];
                        $info->setAttr('name',$v['name']);
                        $info->save();
                    }/* else {
                        $service_project_list[] = [
                            'group_id'=>$group_id,
                            'project_id'=>$v['id'],
                            'category_id'=>$v['category_id'],
                            'name'=>$v['name']
                        ];
                    }*/

                }
               /* if (count($service_project_list)>0){
                    $apply_model->saveAll($service_project_list);
                }*/

            }
            Db::commit();
            return true;
        } catch (\Exception $e) {
            // 回滚事务
            Db::rollback();
            throw new \Exception($e->getMessage());
        }
    }


}
